package utils
{
	import events.ScrollEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
    public class ScrollPanel extends Sprite
    {
        private var _contentHeight:int;
        private var _availableHeight:int;
        private var _verticalPageScrollSize:int = 0;
        private var _availableWidth:int;
        private var _contentClip:Sprite;
        private var _contentScrollRect:Rectangle;
        private var _contentWidth:int;
        private var _source:DisplayObject;
        private var _height:int = 100;
        private var _width:int = 100;
        private var _background:Sprite;
        private var _vScrollBar:ScrollBar;
        private var _contentPadding:int = 0;

        public function ScrollPanel()
        {
            initAll();
        }

        private function onScroll(event:ScrollEvent) : void
        {
            if (!_vScrollBar.visible)
            {
                return;
            }
            setVerticalScrollPosition(event.position);
        }

        private function drawLayout() : void
        {
            var _loc_1:Rectangle = null;
            calculateAvailableSize();
            resetBackground();
            _vScrollBar.x = width - _vScrollBar.width - _contentPadding;
            _vScrollBar.height = _availableHeight;
            _vScrollBar.setScrollProperties(_availableHeight, 0, _contentHeight - _availableHeight, _verticalPageScrollSize);
            _vScrollBar.visible = _contentHeight > _height;
            if (_contentClip.scrollRect)
            {
                _loc_1 = _contentClip.scrollRect;
                _loc_1.height = _availableHeight;
                _loc_1.width = _availableWidth;
            }
            else
            {
                _loc_1 = new Rectangle(0, 0, _availableWidth, _availableHeight);
            }
            _contentClip.scrollRect = _loc_1;
        }

        public function get verticalLineScrollSize() : Number
        {
            return _vScrollBar.lineScrollSize;
        }

        override public function set width(param1:Number) : void
        {
            _width = param1;
            drawLayout();
        }

        public function get horizontalScrollPolicy() : String
        {
            return null;
        }

        override public function get height() : Number
        {
            return _height;
        }

        private function setVerticalScrollPosition(param1:int) : void
        {
            var _loc_2:* = _contentClip.scrollRect;
            if (_loc_2 == null)
            {
                return;
            }
            _loc_2.y = param1;
            _contentClip.scrollRect = _loc_2;
        }

        public function get maxVerticalScrollPosition() : int
        {
            return _vScrollBar.maxScrollPosition;
        }

        public function set source(param1:DisplayObject) : void
        {
            clearSource();
            _source = param1;
            if (_source != null)
            {
                _contentClip.addChild(_source);
            }
            update();
        }

        public function set verticalLineScrollSize(param1:Number) : void
        {
            _vScrollBar.lineScrollSize = param1;
        }

        public function get verticalScrollPosition() : int
        {
            return _vScrollBar.scrollPosition;
        }

        protected function onWheel(event:MouseEvent) : void
        {
            if (!_vScrollBar.visible)
            {
                return;
            }
            _vScrollBar.scrollPosition = _vScrollBar.scrollPosition - (event.delta < 0 ? (-1) : (1)) * _vScrollBar.lineScrollSize;
            setVerticalScrollPosition(_vScrollBar.scrollPosition);
            dispatchEvent(new ScrollEvent(ScrollBarDirection.VERTICAL, event.delta, _vScrollBar.scrollPosition));
        }

        public function get source() : DisplayObject
        {
            return _source;
        }

        private function clearSource() : void
        {
            DisplayUtil.removeAllChild(_contentClip);
        }

        public function update() : void
        {
            var _loc_1:DisplayObject = null;
            if (_contentClip.numChildren > 0)
            {
                _loc_1 = _contentClip.getChildAt(0);
                setContentSize(_loc_1.width, _loc_1.height);
            }
            drawLayout();
        }

        override public function set height(param1:Number) : void
        {
            _height = param1;
            drawLayout();
        }

        private function calculateAvailableSize() : void
        {
            _availableWidth = width - 2 * _contentPadding;
            _availableHeight = height - 2 * _contentPadding;
        }

        public function set horizontalScrollPolicy(param1:String) : void
        {
            
        }

        private function setContentSize(param1:int, param2:int) : void
        {
            _contentWidth = param1;
            _contentHeight = param2;
        }

        private function resetBackground() : void
        {
            if (_background)
            {
                _background.width = width;
                _background.height = height;
            }
        }

        override public function get width() : Number
        {
            return _width;
        }

        public function set verticalScrollPosition(param1:int) : void
        {
            _vScrollBar.setScrollPosition(param1);
        }

        public function set verticalScrollPolicy(param1:String) : void
        {
            
        }

        private function initAll() : void
        {
            initBg();
            _contentClip = new Sprite();
            _vScrollBar = new ScrollBar();
            _vScrollBar.addEventListener(ScrollEvent.SCROLL, onScroll);
            DisplayUtil.addChild(_contentClip, this);
            DisplayUtil.addChild(_vScrollBar, this);
            addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
        }

        public function get verticalScrollPolicy() : String
        {
            return null;
        }

        private function initBg() : void
        {
            _background = new Sprite();
            var _loc_2:* = _background.graphics;
            with (_background.graphics)
            {
                beginFill(0, 0);
                drawRect(0, 0, 100, 100);
                endFill();
            }
            DisplayUtil.addChildAt(_background, this, 0);
        }

    }
}